/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Actors;

import Model.*;
import java.awt.image.BufferedImage;
import java.util.Vector;

/**
 *
 * @author Snowangelic
 */
public class Actor {
    private Vector<ActorListener> listeners;
    protected BufferedImage image;

    protected ActorManager manager;


    private int rayon;
    protected double xPos;
    protected double yPos;
    protected double oldX;
    protected double oldY;

    private int xIndex;
    private int yIndex;
    private int oldXIndex;
    private int oldYIndex;

    protected double dx;
    protected double dy;

    protected double friction;
    private boolean frictionModifiable;

    public Actor(){

        listeners=new Vector <ActorListener>();
        rayon=10;
        xPos=100;
        yPos=100;
        oldX=100;
        oldY=100;
        dx=0;
        dy=0;
        friction=1;
        frictionModifiable=true;
    }

    public void setManager(ActorManager manager){
        this.manager=manager;
    }


    public void addListener(ActorListener listener) {
        if(!listeners.contains(listener))
            listeners.add(listener);

        //listener.actorMoved(new ActorEvent(this));
    }



    public boolean updateImage(long dtime){
        return false;
    }

    public void update(long dtime){
        boolean posChanged=false;
        boolean imageChanged=false;

        //--- Update de l'image ---
        imageChanged=updateImage(dtime);

        //--- Update de la position ---
        xPos=xPos+(dx*dtime/750);
        yPos=yPos+(dy*dtime/750);
        if((int) xPos != (int)oldX || (int) yPos!= (int) oldY) posChanged=true;
        oldX= xPos;
        oldXIndex=xIndex;
        xIndex=(int) xPos/32;
        oldY= yPos;
        oldYIndex=yIndex;
        yIndex=(int) yPos/32;

        //--- Update de la vitesse ---
        double[] impulse=getImpulse();
        dx=(dx/(Math.pow(friction,dtime/3.0))) + (impulse[0]*dtime);
        dy=(dy/(Math.pow(friction,dtime/3.0))) + (impulse[1]*dtime);
        double speed= Math.sqrt((dx*dx) + (dy*dy));
        
        //--- Limitation de la vitesse ---
        if(speed >2200){
            double norm =speed/2200;
            dx=dx/norm;
            dy=dy/norm;
        }

        if(posChanged){
            for(ActorListener listener : listeners)
                listener.actorMoved(new ActorEvent(this));
        }
        else if (imageChanged){
            for(ActorListener listener : listeners)
                listener.actorImageChanged(new ActorEvent(this));
        }
    }

    public void setFriction(double friction){
        if (frictionModifiable)
            this.friction=friction;
    }

    public void setFrictionModifiable (boolean b){
        frictionModifiable=b;
    }
    
    protected double[] getImpulse(){
        double[] impulse=new double[2];
        impulse[0]=0;
        impulse[1]=0;
        return impulse;
    }

    protected void died(){
        for(ActorListener listener : listeners)
            listener.actorDied(new ActorEvent(this));
    }

    public void collidesWith(Actor actor){
        // TODO : collision acteur/acteur
    }

    /*
    ArrayList<Actor> getActorList(){
        return model.getActorList();
    }
     * */


    // --- Getter/setter de base ---
    // <editor-fold defaultstate="collapsed" desc="Code débile">

    public int getX(){
        return (int) xPos;
    }

    public int getXIndex(){
        return xIndex;
    }

    public int getOldX(){
        return (int) oldX;
    }

    public int getOldXIndex(){
        return oldXIndex;
    }

    public void setX(int x){
        xPos=x;
    }

    public int getY(){
        return (int) yPos;
    }

    public int getYIndex(){
        return yIndex;
    }
    public int getOldY(){
        return (int) oldY;
    }

    public int getOldYIndex(){
        return oldYIndex;
    }

    public void setY(int y){
        yPos=y;
    }

    public double getDx(){
        return dx;
    }

    public double getDy(){
        return dy;
    }

    public void setDx(double dx){
        this.dx=dx;
    }

    public void setDy(double dy){
        this.dy=dy;
    }

    public int getRayon(){
        return rayon;
    }

    public BufferedImage getImage(){
        return image;
    }
    // </editor-fold>





}
